package com.jiazhong.servlet.controller;

import com.alibaba.fastjson.JSONArray;
import com.jiazhong.commons.bean.Emp;
import com.jiazhong.servlet.service.EmpService;
import com.jiazhong.servlet.service.impl.EmpServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/emp")
public class EmpController extends BaseController{

    private EmpService service = new EmpServiceImpl();


    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int empNo = Integer.parseInt(request.getParameter("empNo"));
        JsonResult result = service.delete(empNo);
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print(JSONArray.toJSONString(result));
    }
    protected void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        JsonResult all = service.findAll();
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print(JSONArray.toJSONString(all));
    }
    protected void findByEname(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String ename = request.getParameter("ename");
        int page = 1;
        String p = request.getParameter("page");
        if (p != null) {
            page = Integer.parseInt(p);
        }
        JsonResult result = service.findByEname(ename, page);
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print(result);
    }
    protected void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int empNo = Integer.parseInt(request.getParameter("empNo"));
        JsonResult emp = service.findById(empNo);
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.print(emp);
    }
    protected void page(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 1. 获取到用户的请求的参数
        int page = 1, size = 10;
        String p = request.getParameter("page");
        String s = request.getParameter("size");
        if (p != null) {
            page = Integer.parseInt(p);
        }
        if (s != null) {
            size = Integer.parseInt(s);
        }
        // 2. 调用具体的方法
        JsonResult str = service.findByPage(page, size);
        // 3. 响应
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().println(JSONArray.toJSONString(str));
    }
    protected void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        // 1. 接受用户的请求参数
        String ename = request.getParameter("ename");
        System.out.println(ename);
        String job = request.getParameter("job");
        int mgr = Integer.parseInt(request.getParameter("mgr"));
        double sal = Double.parseDouble(request.getParameter("sal"));
        double comm = Double.parseDouble(request.getParameter("comm"));
        int deptNo = Integer.parseInt(request.getParameter("deptNo"));
        Emp emp = new Emp();
        emp.setEname(ename);
        emp.setJob(job);
        emp.setSal(sal);
        emp.setComm(comm);
        emp.setDeptNo(deptNo);
        emp.setMgr(mgr);
        // 2. 调用方法
        JsonResult result = service.save(emp);
        // 3. 返回结果 数据响应
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print(JSONArray.toJSONString(result));
    }
    protected void update(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        int empNo = Integer.parseInt(request.getParameter("empNo"));
        String ename = request.getParameter("ename");
        System.out.println(ename);
        String job = request.getParameter("job");
        int mgr = Integer.parseInt(request.getParameter("mgr"));
        double sal = Double.parseDouble(request.getParameter("sal"));
        double comm = Double.parseDouble(request.getParameter("comm"));
        int deptNo = Integer.parseInt(request.getParameter("deptNo"));
        Emp emp = new Emp();
        emp.setEname(ename);
        emp.setJob(job);
        emp.setSal(sal);
        emp.setComm(comm);
        emp.setDeptNo(deptNo);
        emp.setMgr(mgr);
        emp.setEmpNo(empNo);
        JsonResult result = service.update(emp);
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print(JSONArray.toJSONString(result));
    }
}
